package cn.edu.jnu.experiment1.mapper;

import cn.edu.jnu.experiment1.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user")
    List<User> findAll();

    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Integer id);

    @Select("SELECT * FROM user order by id desc ")
    List<User> findByOrder();

    @Select("SELECT * FROM user WHERE name = #{name}")
    List<User> findByUsername(String name);

    @Select("SELECT * FROM user WHERE username = #{username} and name = #{name} order by id desc")
    List<User> findByMore(String username, String name);

    @Select("SELECT * FROM user WHERE username like concat('%', #{username}, '%') and name like concat('%', #{name}, '%') order by id desc")
    List<User> findByLike(String username, String name);

    @Select("SELECT * FROM user WHERE username like concat('%', #{username}, '%') and name like concat('%', #{name}, '%') order by id desc limit #{skipNum}, #{pageSize}")
    List<User> findByPage(Integer skipNum, Integer pageSize, String username, String name);

    @Select("SELECT count(id) FROM user WHERE username like concat('%', #{username}, '%') and name like concat('%', #{name}, '%') order by id desc")
    int selectCount(String username, String name);

    @Insert("INSERT INTO user(username, password, name, phone, email, address, avatar) VALUES(#{username}, #{password}, #{name}, #{phone}, #{email}, #{address}, #{avatar})")
    int insert(User user);

    @Update("UPDATE user SET username=#{username}, password=#{password}, name=#{name}, phone=#{phone}, email=#{email}, address=#{address}, avatar=#{avatar} WHERE id=#{id}")
    int update(User user);

    @Delete("DELETE FROM user WHERE id=#{id}")
    int delete(Integer id);
    int deleteAll();
}
